﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace ServerMonitoringAssistantWF
{
    public partial class FrmSites : Form
    {
        public FrmSites()
        {
            InitializeComponent();
            dgvMain.MultiSelect = false;
            dgvMain.AllowUserToAddRows = false;

            删除ToolStripMenuItem.Click += cmsMainToolStripItem_Click;
            编辑ToolStripMenuItem.Click += cmsMainToolStripItem_Click;
            刷新ToolStripMenuItem.Click += cmsMainToolStripItem_Click;
            复制ToolStripMenuItem.Click += cmsMainToolStripItem_Click;
            浏览器打开ToolStripMenuItem.Click += cmsMainToolStripItem_Click;
        }

        private void cmsMainToolStripItem_Click(object sender, EventArgs e)
        {
            string sqlstmt;
            switch ((sender as ToolStripMenuItem).Name)
            {
                case "删除ToolStripMenuItem":
                    if (DialogResult.OK != MessageBox.Show("您确定要删除该网站吗?", "提示", MessageBoxButtons.OKCancel))
                    {
                        return;
                    }
                    if (dgvMain.SelectedCells.Count > 0)
                    {
                        int id = int.Parse(dgvMain.SelectedCells[0].OwningRow.Cells["编号"].Value.ToString());
                        sqlstmt = "delete from t_sites where id=@id";
                        DbParameter[] dbparams = new DbParameter[]
                        {
                            SqliteMgr.me.CreateParameterInputInteger("@id",id)
                        };
                        SqliteMgr.me.ExecuteNoneQuery(sqlstmt, dbparams);
                        this.LoadMain();
                    }
                    break;
                case "编辑ToolStripMenuItem":
                    if (dgvMain.SelectedCells.Count > 0)
                    {
                        int id = int.Parse(dgvMain.SelectedCells[0].OwningRow.Cells["编号"].Value.ToString());
                        FrmSiteEdit frmEdit = new FrmSiteEdit() { StartPosition = FormStartPosition.CenterScreen, Owner = this, RecordId = id };
                        frmEdit.Show();
                    }
                    break;
                case "复制ToolStripMenuItem":
                    if (DialogResult.OK != MessageBox.Show("您确定要复制该记录吗?", "确认对话框", MessageBoxButtons.OKCancel))
                    {
                        return;
                    }
                    if (dgvMain.SelectedCells.Count > 0)
                    {
                        int id = int.Parse(dgvMain.SelectedCells[0].OwningRow.Cells["编号"].Value.ToString());
                        sqlstmt = "insert into t_sites (title, url, remark, sort, status) select title||'_New', url, remark, sort, status from t_sites where id=@id";
                        DbParameter[] dbparams = new DbParameter[]
                        {
                            SqliteMgr.me.CreateParameterInputInteger("@id",id)
                        };
                        SqliteMgr.me.ExecuteNoneQuery(sqlstmt, dbparams);
                        this.LoadMain();
                    }
                    break;
                case "刷新ToolStripMenuItem":
                    this.LoadMain();
                    break;
                case "浏览器打开ToolStripMenuItem":
                    if (dgvMain.SelectedCells.Count > 0)
                    {
                        string url = dgvMain.SelectedCells[0].OwningRow.Cells["网址"].Value.ToString();
                        System.Diagnostics.Process.Start(url);
                    }
                    break;
            }
        }

        private void FrmSites_Load(object sender, EventArgs e)
        {
            this.LoadMain();
        }
        public void LoadMain()
        {
            string sqlstmt = "select id as '编号', title as '名称', url as '网址', remark as '备注', sort as '排序', status as '状态', create_time as '时间' from t_sites order by sort, id";
            dgvMain.DataSource = SqliteMgr.me.ExecuteDataTable(sqlstmt);
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            FrmSiteEdit frmEdit = new FrmSiteEdit() { StartPosition = FormStartPosition.CenterScreen, Owner = this };
            frmEdit.Show();
        }

    }
}
